import { GetConnection, GetResult } from "../../util/db.js"
import DiaryModel from "../../model/diaryModel/diaryModel.js"
import { JoinSQLInsertKeys, JoinSQLUpdateKeys } from "../../util/JoinSQL.js"

class DiaryService {
    /** 
     * @param {{userID,startLimit,endLimit}} condition
     */
    static async GetDiaryList(userID, startLimit, endLimit, onlyuser = false) {
        try {

            const conn = await GetConnection()
            let sql = `
SELECT a.id,b.portraitSrc,b.userName,a.address,a.issueTime,a.introduction,a.introductionSrc,b.id as userID
FROM diary a
LEFT JOIN usertable b on a.userID = b.id
LEFT JOIN pet c on a.petID = c.id
WHERE 1 = 1
    AND a.isPassed = 1
            `;
            if (onlyuser) {
                sql += "AND a.userID = ?"
            }
            sql += "order by a.isPassed,a.issueTime "
            const { rows: result } = await GetResult(sql, [[userID]], conn)
            return result
        } catch (error) {
            return Promise.reject(error)
        }
    }

    /** 
     * @param {{userID,startLimit,endLimit}}
     * @returns {Promise<Boolean>}
     * 添加
     */
    static async InsertDiary(condition) {
        try {
            const diary = new DiaryModel(condition)
            const conn = await GetConnection()
            let sql = "insert into diary"
            let { keys, values } = diary.GetKeys_Values()
            sql += JoinSQLInsertKeys(keys)
            const { rows: result } = await GetResult(sql, [[values]], conn, true)
            return result && result.affectedRows;
        } catch (error) {
            return Promise.reject(error)
        }
    }

    /**
     * 修改伴宠日记
     * @param {DiaryModel} model
     * @param {Object} columns
     */
    static async EditDiary(model, columns) {
        try {
            const conn = await GetConnection()
            model = new DiaryModel(model)
            model.SetUpdateColumns(columns)
            let sql = "update diary\t" + JoinSQLUpdateKeys(columns, model)
            let { rows } = await GetResult(sql, undefined, conn, true)
            return rows && rows.affectedRows
        } catch (error) {
            return Promise.reject(error)
        }
    }

    /**
     * @param {string} id
     */
    static async DeleteDiary(id) {
        try {
            const conn = await GetConnection()
            let sql = `
delete diary
where id = ?
            `
        } catch (error) {
            return Promise.reject(error)
        }
    }

    /**
     * @param {boolean} state 查看未审核的
     */
    static async GetAllDiaryList(state = false) {
        try {
            const conn = await GetConnection()
            let sql = `
SELECT a.id,b.portraitSrc,b.userName,a.address,a.issueTime,a.introduction,a.introductionSrc,b.id as userID,a.isPassed
FROM diary a
LEFT JOIN usertable b on a.userID = b.id
LEFT JOIN pet c on a.petID = c.id
WHERE 1 = 1
            `;
            if (state) {
                sql += 'and a.isPassed = 0'
            }
            sql += "\n order by a.isPassed,a.issueTime"
            let { rows } = await GetResult(sql, undefined, conn)
            return rows
        } catch (error) {
            return Promise.reject(error)
        }
    }
}

export default DiaryService